package com.wxtt.android.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fast.dev.frame.glide.BitmapUtils;
import com.fast.dev.frame.glide.GlideLoader;
import com.fast.dev.frame.glide.MulBitmapTransformation;
import com.fast.dev.frame.ui.Bind;
import com.fast.dev.frame.ui.ContentView;
import com.fast.dev.frame.ui.V;
import com.three.WxUtils;
import com.wxtt.android.R;
import com.wxtt.android.bean.VideoBean;
import com.wxtt.android.common.CommonActivity;
import com.wxtt.android.dialog.ShareDialog;
import com.youku.player.base.YoukuBasePlayerManager;
import com.youku.player.base.YoukuPlayer;
import com.youku.player.base.YoukuPlayerView;

import me.gujun.android.taggroup.TagGroup;

/**
 * 说明：视频播放
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/3/7 14:24
 * <p/>
 * 版本：verson 1.0
 */
@ContentView(R.layout.activity_play)
public class VideoPlayActivity extends CommonActivity implements ShareDialog.ShareListener {

    @Bind(id = R.id.youku_play)
    private YoukuPlayerView mYoukuPlayView;
    @Bind(id = R.id.tv_player_count)
    private TextView tv_count;
    @Bind(id = R.id.tv_player_comment)
    private TextView tv_comment;
    @Bind(id = R.id.tv_player_favor)
    private TextView tv_favor;
    @Bind(id = R.id.tv_player_user_name)
    private TextView tv_player_user_name;
    @Bind(id = R.id.tv_player_publish_time)
    private TextView tv_player_publish_time;
    @Bind(id = R.id.iv_player_user_avatar)
    private ImageView iv_user_avatar;
    @Bind(id = R.id.player_tag_group)
    private TagGroup playerTagGroup;

    private YoukuPlayer mYoukuPlay;
    private VideoBean videoBean;
    private Toolbar toolbar;
    private ShareDialog mShareDialog;

    @Override
    public void onInit(Bundle bundle) {
        super.onInit(bundle);
        mBasePlayerManager.onCreate();

        V.setText(tv_count, R.string.view_count, videoBean.getView_count());
        V.setText(tv_comment, R.string.view_comment, videoBean.getComment_count());
        V.setText(tv_favor, R.string.view_favor, videoBean.getFavorite_count());
        V.setText(tv_player_publish_time,R.string.view_video_publish_time,videoBean.getPublished());
        V.setText(tv_player_user_name,videoBean.getUser().getName());

        toolbar.setTitle(videoBean.getTitle());

        playerTagGroup.setTags(videoBean.getTags().split(","));

        GlideLoader.into(videoBean.getUser().getLink(), iv_user_avatar);
        GlideLoader.load(this,videoBean.getUser().getLink().toString(),R.mipmap.ic_avatar,R.mipmap.ic_avatar).transform(new MulBitmapTransformation(this,MulBitmapTransformation.TYPE_CIRCLE)).into(iv_user_avatar);

        initYoukuPlay();
    }

    @Override
    protected boolean showToolbar() {
        return true;
    }


    @Override
    protected int setMenu() {
        return R.menu.video_detail_menu;
    }

    @Override
    protected void onMenuItemClick(int id) {
        super.onMenuItemClick(id);
        switch (id){
            case R.id.detail_meun_share:
                if (mShareDialog == null){
                    mShareDialog = new ShareDialog(this,this);
                }
                mShareDialog.show();
                break;
        }
    }

    @Override
    public int setToolbarTitle() {
        return R.string.ariticle_detail;
    }

    @Override
    protected void onCustomToolbar(View view, Toolbar toolbar) {
        this.toolbar = toolbar;
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void getIntentData(Intent intent) {
        if (intent != null){
            Bundle bundle = intent.getBundleExtra("video");
            if (bundle != null){
                videoBean = bundle.getParcelable("video");
            }
        }
    }

    /**
     * 说明：初始化优酷播放器设置
     */
    private void initYoukuPlay(){
        //控制竖屏和全屏时候的布局参数
        mYoukuPlayView.setSmallScreenLayoutParams(new RelativeLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        mYoukuPlayView
                .setFullScreenLayoutParams(new RelativeLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
        //初始化播放器相关数据
        mYoukuPlayView.initialize(mBasePlayerManager);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mBasePlayerManager.onBackPressed();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mBasePlayerManager.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBasePlayerManager.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean manageKeyDown = mBasePlayerManager.onKeyDown(keyCode,event);
        if (mBasePlayerManager.shouldCallSuperKeyDown()){
            return super.onKeyDown(keyCode,event);
        }else {
            return manageKeyDown;
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mBasePlayerManager.onLowMemory();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mBasePlayerManager.onPause();
    }

    @Override
    public boolean onSearchRequested() {
        return mBasePlayerManager.onSearchRequested();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mBasePlayerManager.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mBasePlayerManager.onStop();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        getIntentData(intent);
        //播放视频
        goPlay();
    }

    /**
     * 说明：播放视频
     */
    private void goPlay(){
        mYoukuPlay.playVideo(videoBean.getId());
    }


    private YoukuBasePlayerManager mBasePlayerManager = new YoukuBasePlayerManager(this) {
        @Override
        public void setPadHorizontalLayout() {

        }

        @Override
        public void onInitializationSuccess(YoukuPlayer player) {
            addPlugins();
            mYoukuPlay = player;
            goPlay();
        }

        @Override
        public void onFullscreenListener() {
            toolbar.setVisibility(View.GONE);
        }

        @Override
        public void onSmallscreenListener() {
            toolbar.setVisibility(View.VISIBLE);
        }
    };

    @Override
    public void shareWechat() {
        if (videoBean != null){
            GlideLoader.bitmap(this, videoBean.getThumbnail(), new GlideLoader.BitmapListener() {
                @Override
                public void onSuccess(Bitmap bitmap) {
                    WxUtils.shareVideo(VideoPlayActivity.this, videoBean.getLink(), videoBean.getTitle(), videoBean.getTitle(), bitmap, WxUtils.SCENE_SESSION);
                }

                @Override
                public void onStart() {

                }

                @Override
                public void onFail(Exception e) {
                    WxUtils.shareVideo(VideoPlayActivity.this, videoBean.getLink(), videoBean.getTitle(), videoBean.getTitle(), BitmapUtils.bitmap(VideoPlayActivity.this
                    ,R.mipmap.app), WxUtils.SCENE_SESSION);
                }
            });

        }
    }

    @Override
    public void shareWxcircle() {
        if (videoBean != null){
            GlideLoader.bitmap(this, videoBean.getThumbnail(), new GlideLoader.BitmapListener() {
                @Override
                public void onSuccess(Bitmap bitmap) {
                    WxUtils.shareVideo(VideoPlayActivity.this, videoBean.getLink(), videoBean.getTitle(), videoBean.getTitle(), bitmap, WxUtils.SCENE_TIMELINE);
                }

                @Override
                public void onStart() {

                }

                @Override
                public void onFail(Exception e) {
                    WxUtils.shareVideo(VideoPlayActivity.this, videoBean.getLink(), videoBean.getTitle(), videoBean.getTitle(), BitmapUtils.bitmap(VideoPlayActivity.this
                            ,R.mipmap.app), WxUtils.SCENE_TIMELINE);
                }
            });

        }
    }
}
